package MyTest;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    Socket socket;
    static Scanner sc = new Scanner(System.in);
    BufferedReader br;
    BufferedWriter bw;
    public Client() throws IOException, InterruptedException {
        //创建通道并连接服务器
         socket = new Socket("MacBook-Pro.local",10000);
        //提示服务器连接成功
        System.out.println("服务器连接成功");
        //开始启动界面
        while (true)
        {
            int choice;
            Init();
            String str = sc.next();
            sc.nextLine();
            try {
                choice = Integer.parseInt(str);
            }catch (NumberFormatException e)
            {
                System.out.println("没有该选项，请重试！");
                continue;
            }
            switch (choice)
            {
                case 1:
                    login();
                    break;
                case 2:
                    register();
                    break;
                case 3:
                    socket.close();
                    return;
                default:
                    System.out.println("没有该选项，请重试！");
            }

        }
    }

    private static void Init() throws InterruptedException {
        Thread.sleep(100);
        //显示初始界面
        System.out.println("============欢迎来到网络聊天室============");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出");
        System.out.println("请输入您的选择：");
    }

    public void login() throws IOException {
        //创建网络输出流
         bw = new BufferedWriter(new OutputStreamWriter
                (socket.getOutputStream()));
        //创建网络输入流
        br = new BufferedReader(new InputStreamReader
                (socket.getInputStream()));


        //输入自己的信息
        System.out.println("请输入用户名：");
        String userName = sc.next();
        if(userName.length()==0)
        {
            System.out.println("用户名不为空，请重新输入");
            return;
        }
        System.out.println("请输入密码：");
        String password = sc.next();
        if(password.length()==0)
        {
            System.out.println("密码不为空，请重新输入");
            return;
        }
        //按下回车，交给服务器进行验证（写到网络输出流）
        write(userName + "=" + password);

        //接收来自服务器的提示信息
        String line = br.readLine();
        System.out.println(line);
        //用户名不存在，直接返回初始界面
        if("用户名不存在".equals(line))
        {
            return;
        }
        //密码错误，直接返回初始界面
        if("密码错误".equals(line))
        {
            return;
        }
        //登录成功，进入聊天窗口的方法
        if("登录成功".equals(line))
        {
            sc.nextLine();
            //聊天开始
            while (true)
            {
                System.out.println("请输入您要说的话：");
                String str = sc.nextLine();
                if(str.length()==0)
                {
                    System.out.println("不能发送空消息");
                    continue;
                }
                write(userName+":"+str);
                String line1 = br.readLine();
                System.out.println(line1);
                if (line1.split(":")[1].equals("886"))
                {
                    break;
                }
            }
        }
    }

    //写入网络输出流
    private void write(String str) throws IOException {
        bw.write(str);
        bw.newLine();
        bw.flush();
    }

    public static void register() throws IOException
    {
        BufferedWriter bwr = new BufferedWriter(new FileWriter("MyTest/userinfo.txt",true));
        System.out.println("请输入用户名");
        String UserName = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();

    }


}
